#include "lineresetwidget.h"
#include "ui_lineresetwidget.h"

#include "common.h"

LineResetWidget::LineResetWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LineResetWidget)
{
    ui->setupUi(this);
    allLines.clear();
    QStringList staggNames;
    staggNames << "all_up" << "all_down" << "odd_up" << "even_up";
    ui->interval_name_staggered->addItems(staggNames);
}

LineResetWidget::~LineResetWidget()
{
    delete ui;
}

void LineResetWidget::initConfig()
{
    AllLineResetWidget *allLineResetWidget = NULL;
    for (int m = 0; m < myapp::mDynamicMapParam->mLineInfoVector.length(); m ++) {
        const LineInfo &line = myapp::mDynamicMapParam->mLineInfoVector.at(m);
        allLineResetWidget = new AllLineResetWidget(this);
        allLineResetWidget->setConfig(line, m + 1);
        ui->allLineResetVerticalLayout->addWidget(allLineResetWidget);
        allLines.push_back(allLineResetWidget);
    }
    allLineResetWidget = NULL;
    ui->all_point_w->setText(QString::number(myapp::mDynamicMapParam->m_station_data.point_width));
    ui->all_point_h->setText(QString::number(myapp::mDynamicMapParam->m_station_data.point_height));
    ui->all_line_w->setText(QString::number(myapp::mDynamicMapParam->m_station_data.line_width));
    ui->all_line_h->setText(QString::number(myapp::mDynamicMapParam->m_station_data.line_height));
    ui->all_name_w->setText(QString::number(myapp::mDynamicMapParam->m_station_data.station_width));
    ui->all_name_h->setText(QString::number(myapp::mDynamicMapParam->m_station_data.station_height));

    ui->interval_begin_x->setText(QString::number(myapp::mDynamicMapParam->m_interval_begin_x));
    ui->interval_begin_y->setText(QString::number(myapp::mDynamicMapParam->m_interval_begin_y));
    ui->interval_end_x->setText(QString::number(myapp::mDynamicMapParam->m_interval_end_x));
    ui->interval_end_y->setText(QString::number(myapp::mDynamicMapParam->m_interval_end_y));
    std::string stag = myapp::getStaggeredFromInt(myapp::mDynamicMapParam->m_interval_staggered);
    ui->interval_name_staggered->setCurrentText(QString::fromStdString(stag));
    ui->interval_name_offset->setText(QString::number(myapp::mDynamicMapParam->m_interval_name_offset));

    ui->interval_point_w->setText(QString::number(myapp::mDynamicMapParam->m_interval_station_data.point_width));
    ui->interval_point_h->setText(QString::number(myapp::mDynamicMapParam->m_interval_station_data.point_height));
    ui->interval_line_w->setText(QString::number(myapp::mDynamicMapParam->m_interval_station_data.line_width));
    ui->interval_line_h->setText(QString::number(myapp::mDynamicMapParam->m_interval_station_data.line_height));
    ui->interval_name_w->setText(QString::number(myapp::mDynamicMapParam->m_interval_station_data.station_width));
    ui->interval_name_h->setText(QString::number(myapp::mDynamicMapParam->m_interval_station_data.station_height));
}

void LineResetWidget::on_pushButton_clicked()
{
    AllLineResetWidget *allLineResetWidget = NULL;
    LineInfo line;
    myapp::mDynamicMapParam->mLineInfoVector.clear();
    for (int m = 0; m < allLines.length(); m ++) {
        allLineResetWidget = allLines.at(m);
        allLineResetWidget->getConfig(line);
        myapp::mDynamicMapParam->mLineInfoVector.push_back(line);
    }
    allLineResetWidget = NULL;
    myapp::mDynamicMapParam->m_station_data.point_width = ui->all_point_w->text().toInt();
    myapp::mDynamicMapParam->m_station_data.point_height = ui->all_point_h->text().toInt();
    myapp::mDynamicMapParam->m_station_data.line_width = ui->all_line_w->text().toInt();
    myapp::mDynamicMapParam->m_station_data.line_height = ui->all_line_h->text().toInt();
    myapp::mDynamicMapParam->m_station_data.station_width = ui->all_name_w->text().toInt();
    myapp::mDynamicMapParam->m_station_data.station_height = ui->all_name_h->text().toInt();

    myapp::mDynamicMapParam->m_interval_begin_x = ui->interval_begin_x->text().toInt();
    myapp::mDynamicMapParam->m_interval_begin_y = ui->interval_begin_y->text().toInt();
    myapp::mDynamicMapParam->m_interval_end_x = ui->interval_end_x->text().toInt();
    myapp::mDynamicMapParam->m_interval_end_y = ui->interval_end_y->text().toInt();
    myapp::mDynamicMapParam->m_interval_staggered = myapp::getStaggeredFromString(ui->interval_name_staggered->currentText().toStdString());
    myapp::mDynamicMapParam->m_interval_name_offset = ui->interval_name_offset->text().toInt();

    myapp::mDynamicMapParam->m_interval_station_data.point_width = ui->interval_point_w->text().toInt();
    myapp::mDynamicMapParam->m_interval_station_data.point_height = ui->interval_point_h->text().toInt();
    myapp::mDynamicMapParam->m_interval_station_data.line_width = ui->interval_line_w->text().toInt();
    myapp::mDynamicMapParam->m_interval_station_data.line_height = ui->interval_line_h->text().toInt();
    myapp::mDynamicMapParam->m_interval_station_data.station_width = ui->interval_name_w->text().toInt();
    myapp::mDynamicMapParam->m_interval_station_data.station_height = ui->interval_name_h->text().toInt();

    emit reload_from_config();
}

